<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="1可用0被删除-1被禁用" prop="live">
              <el-input v-model="queryParams.live" placeholder="请输入1可用0被删除-1被禁用" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标书内容" prop="text">
              <el-input v-model="queryParams.text" placeholder="请输入标书内容" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标题" prop="head">
              <el-input v-model="queryParams.head" placeholder="请输入标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="页数" prop="pages">
              <el-input v-model="queryParams.pages" placeholder="请输入页数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="字数" prop="words">
              <el-input v-model="queryParams.words" placeholder="请输入字数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="图文并茂，默认开启1" prop="imageText">
              <el-input v-model="queryParams.imageText" placeholder="请输入图文并茂，默认开启1" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="开启deepseek，默认1开启" prop="ds">
              <el-input v-model="queryParams.ds" placeholder="请输入开启deepseek，默认1开启" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="风格，例如技术风、服务风" prop="style">
              <el-input v-model="queryParams.style" placeholder="请输入风格，例如技术风、服务风" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="引用材料的列表" prop="quote">
              <el-input v-model="queryParams.quote" placeholder="请输入引用材料的列表" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目概述" prop="overview">
              <el-input v-model="queryParams.overview" placeholder="请输入项目概述" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="评分点" prop="score">
              <el-input v-model="queryParams.score" placeholder="请输入评分点" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="目录" prop="catalogue">
              <el-input v-model="queryParams.catalogue" placeholder="请输入目录" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="被下载次数" prop="download">
              <el-input v-model="queryParams.download" placeholder="请输入被下载次数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="表格量" prop="tables">
              <el-input v-model="queryParams.tables" placeholder="请输入表格量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:document:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:document:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:document:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:document:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table 
        v-loading="loading" 
        :data="documentList" 
        @selection-change="handleSelectionChange"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标书用户主键id" align="center" prop="docId" v-if="true" />
        <el-table-column label="1可用0被删除-1被禁用" align="center" prop="live" />
        <el-table-column label="标书内容" align="center" prop="text" />
        <el-table-column label="标题" align="center" prop="head" />
        <el-table-column label="页数" align="center" prop="pages" />
        <el-table-column label="字数" align="center" prop="words" />
        <el-table-column label="图文并茂，默认开启1" align="center" prop="imageText" />
        <el-table-column label="开启deepseek，默认1开启" align="center" prop="ds" />
        <el-table-column label="风格，例如技术风、服务风" align="center" prop="style" />
        <el-table-column label="标书类型，企业采购、政府招标" align="center" prop="type" />
        <el-table-column label="引用材料的列表" align="center" prop="quote" />
        <el-table-column label="项目概述" align="center" prop="overview" />
        <el-table-column label="评分点" align="center" prop="score" />
        <el-table-column label="目录" align="center" prop="catalogue" />
        <el-table-column label="被下载次数" align="center" prop="download" />
        <el-table-column label="属于哪个用户" align="center" prop="uid" v-if="true" />
        <el-table-column label="表格量" align="center" prop="tables" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:document:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:document:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改标书内容对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="documentFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="1可用0被删除-1被禁用" prop="live">
          <el-input v-model="form.live" placeholder="请输入1可用0被删除-1被禁用" />
        </el-form-item>
        <el-form-item label="标书内容" prop="text">
            <el-input v-model="form.text" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="标题" prop="head">
          <el-input v-model="form.head" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="页数" prop="pages">
          <el-input v-model="form.pages" placeholder="请输入页数" />
        </el-form-item>
        <el-form-item label="字数" prop="words">
          <el-input v-model="form.words" placeholder="请输入字数" />
        </el-form-item>
        <el-form-item label="图文并茂，默认开启1" prop="imageText">
          <el-input v-model="form.imageText" placeholder="请输入图文并茂，默认开启1" />
        </el-form-item>
        <el-form-item label="开启deepseek，默认1开启" prop="ds">
          <el-input v-model="form.ds" placeholder="请输入开启deepseek，默认1开启" />
        </el-form-item>
        <el-form-item label="风格，例如技术风、服务风" prop="style">
          <el-input v-model="form.style" placeholder="请输入风格，例如技术风、服务风" />
        </el-form-item>
        <el-form-item label="引用材料的列表" prop="quote">
            <el-input v-model="form.quote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="项目概述" prop="overview">
            <el-input v-model="form.overview" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评分点" prop="score">
            <el-input v-model="form.score" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="目录" prop="catalogue">
            <el-input v-model="form.catalogue" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="被下载次数" prop="download">
          <el-input v-model="form.download" placeholder="请输入被下载次数" />
        </el-form-item>
        <el-form-item label="表格量" prop="tables">
          <el-input v-model="form.tables" placeholder="请输入表格量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Document" lang="ts">
import { listDocument, getDocument, delDocument, addDocument, updateDocument } from '@/api/system/document';
import { DocumentVO, DocumentQuery, DocumentForm } from '@/api/system/document/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const documentList = ref<DocumentVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const documentFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: DocumentForm = {
  docId: undefined,
  live: undefined,
  text: undefined,
  head: undefined,
  pages: undefined,
  words: undefined,
  imageText: undefined,
  ds: undefined,
  style: undefined,
  type: undefined,
  quote: undefined,
  overview: undefined,
  score: undefined,
  catalogue: undefined,
  download: undefined,
  uid: undefined,
  tables: undefined
}
const data = reactive<PageData<DocumentForm, DocumentQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    live: undefined,
    text: undefined,
    head: undefined,
    pages: undefined,
    words: undefined,
    imageText: undefined,
    ds: undefined,
    style: undefined,
    type: undefined,
    quote: undefined,
    overview: undefined,
    score: undefined,
    catalogue: undefined,
    download: undefined,
    tables: undefined,
    params: {
    }
  },
  rules: {
    docId: [
      { required: true, message: "标书用户主键id不能为空", trigger: "blur" }
    ],
    uid: [
      { required: true, message: "属于哪个用户不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询标书内容列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDocument(queryParams.value);
  documentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  documentFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: DocumentVO[]) => {
  ids.value = selection.map(item => item.docId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加标书内容";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: DocumentVO) => {
  reset();
  const _docId = row?.docId || ids.value[0]
  const res = await getDocument(_docId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改标书内容";
}

/** 提交按钮 */
const submitForm = () => {
  documentFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.docId) {
        await updateDocument(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addDocument(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: DocumentVO) => {
  const _docIds = row?.docId || ids.value;
  await proxy?.$modal.confirm('是否确认删除标书内容编号为"' + _docIds + '"的数据项？').finally(() => loading.value = false);
  await delDocument(_docIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/document/export', {
    ...queryParams.value
  }, `document_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.p-2 {
  background: var(--ai-bg-primary);
  min-height: 100vh;
  padding: 20px;
}

:deep(.el-card) {
  background: var(--ai-bg-card) !important;
  backdrop-filter: var(--ai-blur-md);
  border: 1px solid var(--ai-border) !important;
  border-radius: var(--ai-radius-lg) !important;
  box-shadow: var(--ai-shadow-lg) !important;
}

:deep(.el-card__header) {
  background: var(--ai-gradient-subtle) !important;
  border-bottom: 1px solid var(--ai-border) !important;
}

:deep(.el-button--primary) {
  background: var(--ai-gradient-primary) !important;
  border: none !important;
  color: white !important;
  box-shadow: var(--ai-shadow-md) !important;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: var(--ai-shadow-lg) !important;
  }
}

:deep(.el-table) {
  background: var(--ai-bg-glass) !important;
  backdrop-filter: var(--ai-blur-sm);
  border-radius: var(--ai-radius-md) !important;
  overflow: hidden;
}

:deep(.el-table__header) {
  background: var(--ai-bg-overlay) !important;
}

:deep(.el-table__header th) {
  background: transparent !important;
  color: var(--ai-text-secondary) !important;
}

:deep(.el-table__body tr:hover) {
  background: var(--ai-bg-overlay) !important;
}

:deep(.el-dialog) {
  border-radius: var(--ai-radius-lg) !important;
  overflow: hidden;
}

:deep(.el-dialog__header) {
  background: var(--ai-gradient-subtle) !important;
  border-bottom: 1px solid var(--ai-border) !important;
}

:deep(.el-input__wrapper) {
  background: var(--ai-bg-glass) !important;
  border: 2px solid var(--ai-border) !important;
  border-radius: var(--ai-radius-md) !important;
  
  &:hover {
    border-color: var(--ai-primary-light) !important;
    box-shadow: var(--ai-shadow-sm) !important;
  }
  
  &.is-focus {
    border-color: var(--ai-primary) !important;
    box-shadow: var(--ai-shadow-md) !important;
  }
}
</style>
